/*	Copyright 2008 Jacquemin Valentin

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
	
	@author pollux
 */
package org.poxd.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;

import org.poxd.syntax.JEditTextArea;
import org.poxd.syntax.TokenMarker;

/**
 * @author pollux
 * 
 */
public class SniweTextArea extends JEditTextArea {
	private static final long serialVersionUID = 1L;
	private static final String MARKERS_PACKAGE = "org.poxd.syntax";
	private static final String SUFFIX_MARKER = "TokenMarker";

	public SniweTextArea() {
		Font defaultFont = new Font("Monospaced", Font.PLAIN, 10);
		setFont(defaultFont);

		FontMetrics fm = getFontMetrics(getFont());
		setMinimumSize(new Dimension(40 * fm.charWidth('m'), 5 * fm.getHeight()));
		setPreferredSize(new Dimension(80 * fm.charWidth('m'), 15 * fm
				.getHeight()));

		inputHandler.addKeyBinding("C+C", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				copy();
			}
		});
		inputHandler.addKeyBinding("C+V", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				paste();
			}
		});
		inputHandler.addKeyBinding("C+A", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				selectAll();
			}
		});
	}

	/**
	 * @param markerName
	 */
	public void setMarker(String markerName) {
		String className = MARKERS_PACKAGE + "." + markerName + SUFFIX_MARKER;
		try {
			TokenMarker marker = (TokenMarker) Class.forName(className)
					.newInstance();
			setTokenMarker(marker);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public class MyFocusListener implements FocusListener {

		public void focusGained(FocusEvent e) {
			System.out.println("ok");
		}

		public void focusLost(FocusEvent e) {
		}

	}

	public class MyKeyListener implements KeyListener {
		{
			System.out.println("created");
		}

		public void keyPressed(KeyEvent e) {
			System.out.println(e);
			if (e.getKeyCode() == KeyEvent.VK_C) {
				System.out.println("c");
				e.consume();
			}
		}

		public void keyReleased(KeyEvent e) {
			System.out.println(e);
			if (e.getKeyCode() == KeyEvent.VK_C) {
				System.out.println("c");
				e.consume();
			}
		}

		public void keyTyped(KeyEvent e) {
			System.out.println(e);
			if (e.getKeyCode() == KeyEvent.VK_C) {
				System.out.println("c");
				e.consume();
			}
		}

	}
}
